/******************************************************************************\
*  Animeshell 1.0 Multimedia manager                                           *
*  Copyright (C) 2006-2007 Rémy Guillemette <shaikadzari@gmail.com>            *
*                                                                              *
*  This program is free software; you can redistribute it and/or modify        *
*  it under the terms of the GNU General Public License version 2 only as      *
*  published by the Free Software Foundation.                                  *
*                                                                              *
*  This program is distributed in the hope that it will be useful,             *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
*  GNU General Public License for more details.                                *
*                                                                              *
*  You should have received a copy of the GNU General Public License           *
*  along with this program; if not, write to the                               *
*  Free Software Foundation, Inc.,                                             *
*  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                   *
\******************************************************************************/

#include "frmAnimeshellimpl.h"
#include "rechercheexterne.h"
#include "about.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QTreeWidgetItem>
#include <QVariant>
#include <QPixmap>
#include <QSize>
#include <QTextEdit>
#include <QComboBox>
#include <QFileInfo>

#include <QDebug>

//
MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f) 
	: QMainWindow(parent, f)
{
	setupUi(this);
	
	// Connected the slots/signals
	connect(btnEditImg, SIGNAL(clicked()), this, SLOT(btnEditImg_Clicked()));
	connect(btnAddImg, SIGNAL(clicked()), this, SLOT(btnAddImg_Clicked()));
	connect(btnSupImage, SIGNAL(clicked()), this, SLOT(btnSupImage_Clicked()));
	connect(btnSearch, SIGNAL(clicked()), this, SLOT(btnSearch_Clicked()));
	connect(btnSaveEntry, SIGNAL(clicked()), this, SLOT(btnSaveEntry_Clicked()));
	connect(btnEraseEntry, SIGNAL(clicked()), this, SLOT(btnEraseEntry_Clicked()));
	connect(actionRecharger, SIGNAL(triggered()), this, SLOT(actionRecharger_Triggered()));
	connect(actionQuitter, SIGNAL(triggered()), this, SLOT(actionQuitter_Triggered()));
	connect(actionRecherche_interne, SIGNAL(triggered()), this, SLOT(actionRecherche_interne_Triggered()));
	connect(actionRecherche_externe, SIGNAL(triggered()), this, SLOT(actionRecherche_externe_Triggered()));
	connect(actionGoReference, SIGNAL(triggered()), this, SLOT(actionGoReference_Triggered()));
	connect(actionNouvelleEntre, SIGNAL(triggered()), this, SLOT(actionNouvelleEntre_Triggered()));
	connect(actionModifierEntre, SIGNAL(triggered()), this, SLOT(actionModifierEntre_Triggered()));
	connect(actionSupprimerEntre, SIGNAL(triggered()), this, SLOT(btnEraseEntry_Clicked()));
	connect(actionPropos, SIGNAL(triggered()), this, SLOT(actionPropos_Triggered()));
	connect(actionAide, SIGNAL(triggered()), this, SLOT(actionAide_Triggered()));
	connect(actionProposQT, SIGNAL(triggered()), this, SLOT(actionProposQT_Triggered()));
	connect(actionEditerImage, SIGNAL(triggered()), this, SLOT(btnEditImg_Clicked()));
	connect(actionStartPlayer, SIGNAL(triggered()), this, SLOT(actionStartPlayer_Triggered()));
	connect(cboFilter, SIGNAL(currentIndexChanged (int)), this, SLOT(cboFilterChanged()));
	connect(cboProfil, SIGNAL(currentIndexChanged (int)), this, SLOT(cboProfilChanged()));
	//
	
	// Treewidget
	trvList->setColumnCount(2);
	trvList->setHeaderLabels ( QStringList () << tr("No.") << tr("Nom") );
	trvList->hideColumn(0);
	connect(trvList, SIGNAL(clicked ( QModelIndex ) ), this, SLOT(TreeWidgetOnClickEvent()));
	
	// Disable btnSave, btnAddImg, btnEditImg
	btnSaveEntry->setEnabled(false);
	btnAddImg->setEnabled(false);
	btnEditImg->setEnabled(false);
	
	m_ActionNewEntry = false;
}

bool MainWindowImpl::initialload()
{
	// Create animeshellrc class handle configuration file
	m_AnimeShellRc = new animeshellrc(cboProfil->itemText(0), cboProfil->itemText(1), cboProfil->itemText(2));
	
	// Load or create profile
	m_CurrentProfile = m_AnimeShellRc->GetQSettings(QString::null);
	
	if ( m_CurrentProfile.isEmpty() )
		return false;
	
	cboProfilChanged();
	
	return true;
}

bool MainWindowImpl::initialdatabase()
{
	QString TableName( m_CurrentProfile[folder] + "/AnimeshellSQLite" );
	
	if ( !QFile::exists(TableName) )
	{
		QFile::copy ( ":/bd/resources/AnimeshellSQLite", TableName);
		QFile::setPermissions( TableName, QFile::ReadUser | QFile::WriteUser | QFile::ReadOwner | QFile::WriteOwner);
	}
	
	// Create mysqliteclass class to handle sqlite communication
	m_MySqliteClass = new mysqliteclass("QSQLITE", TableName, "animeshell", "animeshell");
	
	if ( !m_MySqliteClass->OpenDatabase() )
		return false;
		
	
	// Load animedb table
	m_AnimeList = m_MySqliteClass->GetLineFromTable();
	
	// Put the data in the treeWidget
	//PopulateTreeView();
	cboFilterChanged();
	
	return true;
}

void MainWindowImpl::btnEditImg_Clicked()
{
	// Variables
	QTreeWidgetItem *SelectIndex;
	int IndNoSeq;
	animeclass a;
	QString s(m_CurrentProfile[folder]);
	QString Path;
	
	
	// Get current item
	if ( !m_Items.isEmpty() )
	{
		SelectIndex = trvList->currentItem();
	
		// Get entry primary key
		IndNoSeq = (QVariant(SelectIndex->data(0, Qt::DisplayRole))).toInt();
		
		// Get animeclass object
		a = AnimeTable( IndNoSeq );
		
		// Load image in editor
		Path = QString("file://") + m_CurrentProfile[folder] + "/images/" + a.getImageFile();
		imgcontrol::startprocess( Path );
	}
	else
		QMessageBox::warning(0, tr("Erreur"), tr("La collection est vide"), QMessageBox::Ok, QMessageBox::NoButton);
}

void MainWindowImpl::btnAddImg_Clicked()
{
	// Variable
	QString NewImgName;
	QString imgPath;
	int Pk;
	QFileInfo ext;
	
	if ( txtName->text() != "" )
	{
		// Open File Dialog to choose image
		imgPath = QFileDialog::getOpenFileName(this, tr("Ouvrir une image"), 
											   QDir::homePath(), "Image Files (*.png *.jpg *.jpeg *gif *.bmp)");
		
		// Remember the name
		if ( imgPath != "" )
		{
			m_AddImageName = imgPath;
			
			// Show image
			QPixmap pixmap( m_AddImageName, 0, Qt::ThresholdDither);
			
			// Scale the picture to 292-256
			QSize s(256,269);
			pixmap = pixmap.scaled(s, Qt::KeepAspectRatio, Qt::SmoothTransformation);
			
			// Load image
			imgImageFile->setPixmap(pixmap);
		
			// Get file extension
			ext = QFileInfo(m_AddImageName);
			
			// Add next primary key
			if ( !m_AnimeList.isEmpty() )
				Pk = (m_AnimeList.last()).getNoSeq() + 1;
			else
				Pk = 1;
				
			// Create new name before copy
			NewImgName = QString::number(Pk) + "-" + txtName->text() + "." + ext.completeSuffix();
			
			// Show new name
		    txtImageFile->setText( NewImgName );
		}
	}
	else
	{
		QMessageBox::information(0, tr("Champ obligatoire"), 
				tr("Vous devez saisir le nom de l'entrée avant de choisir l'image"),
                QMessageBox::Ok, QMessageBox::Ok);
	}
	
	
}

void MainWindowImpl::btnSupImage_Clicked()
{
	int IndNoSeq;
	QString path( m_CurrentProfile[folder] + "/images/" + txtImageFile->text() );
	animeclass a;
	QTreeWidgetItem *SelectIndex = trvList->currentItem();
	
	IndNoSeq = (QVariant(SelectIndex->data(0, Qt::DisplayRole))).toInt();
	
	a = AnimeTable(IndNoSeq);
	
	if ( txtImageFile->text() == "" )
	{
		QMessageBox::information(0, tr("Champ obligatoire"), 
				tr("Cette entrée n'a aucune image"),
                QMessageBox::Ok, QMessageBox::Ok);
        return;		
	}	
	
	// Clear the textbox
	txtImageFile->setText("");
	
	// Update Database
	a.setImageFile("");
	m_MySqliteClass->UpdateTable(a);
	
	// Remove the file from the animeshell directory
	if ( !imgcontrol::eraseimg(path) )
	{
		QMessageBox::warning(0, tr("Erreur"), tr("Impossible d'effacer ") + path,QMessageBox::Ok, QMessageBox::NoButton);
	}
}

void MainWindowImpl::btnSearch_Clicked()
{
	// Variables
	animeclass a;
	bool strict = false;
	
	if ( chkStrict->checkState() == Qt::Checked )
		strict = true;
	
	// Build animeclass object to search
	a.setName( txtSearchNom->text() );
	a.setIndice( txtSearchInd->text().toInt() );
	a.setTypeData( cboSearchType->currentIndex() );
	a.setGenreData1( cboSearchGenre->currentIndex() );
	
	SearchInAnimeList( a, strict );
}

void MainWindowImpl::btnSaveEntry_Clicked()
{
	animeclass a;
	QTreeWidgetItem *SelectIndex;
	int IndNoSeq;
	
	// Create animeclass object
	a.setName( txtName->text() );
	if ( txtInd->text() != "" )
		a.setIndice( txtInd->text().toInt() );
	else
		a.setIndice( 0 );
	a.setTypeData( cboTypeData->currentIndex() );
	a.setGenreData1( cboGenreData1->currentIndex() );
	a.setGenreData2( cboGenreData2->currentIndex() );
	a.setTypeSupport( cboSupport->currentIndex() );
	a.setQuotation( cboQuote->currentIndex() );
	a.setDescription( txtDesc->toPlainText() );
	a.setReference( txtReference->text() );
	a.setImageFile( txtImageFile->text() );
		
	
	if ( !m_ActionNewEntry )
	{
		SelectIndex = trvList->currentItem();
		IndNoSeq = (QVariant(SelectIndex->data(0, Qt::DisplayRole))).toInt();
		a.setNoSeq( IndNoSeq );
	}
	else
	{
		a.setNoSeq( -1 );
	}
		
	if ( !AddObjectToList(a, m_ActionNewEntry) )
	{	
		QMessageBox::warning(0, tr("Erreur"), tr("Erreur lors de l'ajout"),QMessageBox::Ok, QMessageBox::NoButton);
	}
	
	m_ActionNewEntry = false;
	btnSaveEntry->setEnabled(false);
}
	
void MainWindowImpl::btnEraseEntry_Clicked()
{
	// Variable
	int IndNoSeq;
	int Pos;
	
	if ( !m_Items.isEmpty() )
	{
		QTreeWidgetItem *SelectIndex = trvList->currentItem();
			
			
		IndNoSeq = (QVariant(SelectIndex->data(0, Qt::DisplayRole))).toInt();
		
		// We need to be sure we want to erase this entry
		int qmb = QMessageBox::question( this, tr("Effacer cette entrée"), 
										 tr("Êtes-vous sûr de vouloir effacer cette entrée?\n\n")  
										 + tr("Numéro : ") + (QVariant(SelectIndex->data(0, Qt::DisplayRole))).toString() + "\n" 
										 + tr("Nom : ") + (QVariant(SelectIndex->data(1, Qt::DisplayRole))).toString(),
		 								 QMessageBox::Yes | QMessageBox::No , QMessageBox::No);
		 	
		if ( qmb == QMessageBox::Yes )
		{
			// Erase from QList
			 Pos = AnimeTablePosition(IndNoSeq);
			 m_AnimeList.removeAt(Pos);
			 	
			 // Erase from animedb table
			 m_MySqliteClass->DeleteLineFromTable(IndNoSeq);
			
			// Refresh QTreeWidget
		 	PopulateTreeView();
	 	}
	}
	else
		QMessageBox::warning(0, tr("Erreur"), tr("La collection est vide"), QMessageBox::Ok, QMessageBox::NoButton);
	
	
}


void MainWindowImpl::actionRecharger_Triggered()
{
	cboFilterChanged();
}


void MainWindowImpl::actionRecherche_interne_Triggered()
{
	txtSearchNom->setText("Recherche?");
	txtSearchNom->selectAll();
	txtSearchNom->setFocus();
}


void MainWindowImpl::actionRecherche_externe_Triggered()
{
	QStringList webSearch;
	QStringList list = m_AnimeShellRc->getListProfile();
	// Declare the form
	rechercheexterne ex;
	
	// Get websearch
	for ( int i = 0; i < list.size() ; i ++ )
	{
		webSearch << m_AnimeShellRc->GetQSettingsChild(list[i], "websearch");
	}
	
	// Setup websearch
	ex.setWebSearch(webSearch);
	
	// Setup Browser
	ex.setWebBrowser(m_CurrentProfile[browser]);
	
	// Launch it
	ex.exec();
}
void MainWindowImpl::actionGoReference_Triggered()
{
	if ( txtReference->text() != "" )
	{
		imgcontrol::startprocess(txtReference->text().toUtf8() );
	}
	else
		QMessageBox::warning(0, tr("Erreur"), tr("Aucune référence disponible"), QMessageBox::Ok, QMessageBox::NoButton);     
	
}
void MainWindowImpl::actionPropos_Triggered()
{
	about a;
	a.exec();
}
void MainWindowImpl::actionAide_Triggered()
{
	// Help on guidelinux
	imgcontrol::startprocess( "http://www.guidelinux.org/phpBB/index.php" );
}
void MainWindowImpl::actionProposQT_Triggered()
{
	QApplication::aboutQt () ;
}
void MainWindowImpl::actionStartPlayer_Triggered()
{
	// Execute current player from profile
	imgcontrol::startprocess(m_CurrentProfile[player], QString::null );
}
void MainWindowImpl::actionQuitter_Triggered()
{
	m_MySqliteClass->CloseDatabase();
	this->close();
}
void MainWindowImpl::actionNouvelleEntre_Triggered()
{
	// Prepare the gui
	txtName->setText("");
	txtInd->setText("");
	txtReference->setText("");
	txtDesc->setText("");
	txtImageFile->setText("");
	cboTypeData->setCurrentIndex(0);
	cboGenreData1->setCurrentIndex(0);
	cboGenreData2->setCurrentIndex(0);
	cboSupport->setCurrentIndex(0);
	imgImageFile->clear();
	
	btnAddImg->setEnabled(true);
	btnEditImg->setEnabled(false);
	btnSaveEntry->setEnabled(true);
	
	m_ActionNewEntry = true;
}
void MainWindowImpl::actionModifierEntre_Triggered()
{
	if ( !m_Items.isEmpty() )
		btnSaveEntry->setEnabled(true);
}

void MainWindowImpl::TreeWidgetOnClickEvent()
{
	int IndNoSeq;
	animeclass a;
	QTreeWidgetItem *SelectIndex = trvList->currentItem();
	
	IndNoSeq = (QVariant(SelectIndex->data(0, Qt::DisplayRole))).toInt();
	
	a = AnimeTable(IndNoSeq);
	
	txtName->setText(a.getName());
	txtInd->setText( QString::number(a.getIndice()) );
	txtReference->setText(a.getReference());
	txtReference->setCursorPosition(0);
	txtDesc->setText(a.getDescription());
	if ( a.getImageFile() != "")
	{
		txtImageFile->setText(a.getImageFile());
		btnAddImg->setEnabled(false);
		btnEditImg->setEnabled(true);
		btnSupImage->setEnabled(true);
	}
	else
	{
		btnAddImg->setEnabled(true);
		btnSupImage->setEnabled(false);
	}
	
	cboGenreData1->setCurrentIndex(a.getGenreData1());
	cboGenreData2->setCurrentIndex(a.getGenreData2());
	cboTypeData->setCurrentIndex(a.getTypeData());
	cboSupport->setCurrentIndex(a.getTypeSupport());
	cboQuote->setCurrentIndex(a.getQuotation());
	
	LoadImageFile(a.getImageFile());
	
	if ( m_ActionNewEntry )
	{
		m_ActionNewEntry = false;
		btnSaveEntry->setEnabled(false);
	}
		
}

QList<animeclass> MainWindowImpl::SearchInAnimeList(animeclass pSearch, bool pStrict)
{	
	// Variables
	int Genre;
	int Type;
	int Ind;
	
	// Clear the previous list
	m_SearchList.clear();
	
	// We search with the name if it is not null
	if ( pSearch.getName() != "" )
	{
		for ( int i = 0; i < m_AnimeList.size() ; i ++ )
		{
			if ( SearchString(pSearch.getName().toUtf8(), (m_AnimeList[i].getName()).toUtf8(), pStrict) )
			{
				m_SearchList.append(m_AnimeList[i]);
			}
		}
	}
	else // Without name, we use combobox informations and ind
	{
		Genre = pSearch.getGenreData1();
		Type = pSearch.getTypeData();
		Ind = pSearch.getIndice();
		
		for ( int i = 0; i < m_AnimeList.size() ; i ++ )			
		{
			if ( Ind == (m_AnimeList[i]).getIndice() || Type == (m_AnimeList[i]).getTypeData() || Genre == (m_AnimeList[i]).getGenreData1() || Genre == (m_AnimeList[i]).getGenreData2() )
			{
				m_SearchList.append(m_AnimeList[i]);
			}
		}
	}
	
	// Change the filter and launch the signal
	if ( cboFilter->currentIndex() != 1 )
		cboFilter->setCurrentIndex(1);
	else
		PopulateTreeView( 1 );
	
	return m_SearchList;
}

bool MainWindowImpl::SearchString(QString pSearch, QString pEntry, bool pStrict)
{
	bool strfind = false;
	
	if ( pStrict )
	{		
		if ( pSearch.toUpper() == pEntry.toUpper() )
			strfind = true;
	}
	else
	{
		
		if ( pEntry.contains(pSearch, Qt::CaseInsensitive ) )
			strfind = true;
	}
	
	return strfind;
}

animeclass MainWindowImpl::AnimeTable(int pInd)
{
	bool boucle = true;
	int i = 0;
	animeclass a;
	
	while ( boucle && i < m_AnimeList.size() )
	{
		if ( ((animeclass)m_AnimeList.at(i)).getNoSeq() == pInd )
		{
			return ((animeclass)m_AnimeList.at(i));
			boucle = false;
		}
		i++;
	}
	
	return a;	
}

int MainWindowImpl::AnimeTablePosition(int pInd)
{
	bool boucle = true;
	int i = 0;
	int ret = -1;
	
	while ( boucle && i < m_AnimeList.size() )
	{
		if ( ((animeclass)m_AnimeList.at(i)).getNoSeq() == pInd )
		{
			ret = i;
			boucle = false;
		}
		i++;
	}
	
	return ret;	
}

void MainWindowImpl::LoadImageFile(QString pImgName)
{
	QString img;
	if ( pImgName != "" )
	{
		img = pImgName;

		QString path( m_CurrentProfile[folder] + "/images/" + img );
		QPixmap pixmap( path, 0, Qt::ThresholdDither);
		
		// Scale the picture
		QSize s(256,269);
		pixmap = pixmap.scaled(s, Qt::KeepAspectRatio, Qt::SmoothTransformation);
		
		// Load image
		imgImageFile->setPixmap(pixmap);
	}
}

bool MainWindowImpl::AddObjectToList(animeclass pA, bool pNewEntry)
{
	bool ret = false;
	int i = 0;
	int Pk;
	
	if ( pNewEntry ) // New entry
	{
		// Add in QList
		m_AnimeList.append(pA);
		
		// Insert in table
		m_MySqliteClass->SetNewLineInTable(pA);
		Pk = (m_MySqliteClass->GetLastInsert()).getNoSeq();
		
		// Copy image
		imgcontrol::copyimg( m_AddImageName, m_CurrentProfile[folder] + "/images/", txtImageFile->text() );
		
		// Setting up the primary key
		(m_AnimeList.last()).setNoSeq(Pk);
		
		m_AddImageName.clear();
		ret = true;
	}
	else // Modified entry
	{
		// Get the position
		i = AnimeTablePosition(pA.getNoSeq());
		
		// Change the entry in m_AnimeList
		m_AnimeList.replace(i, pA);
		
		// Update table
		m_MySqliteClass->UpdateTable(pA);
		
		ret = true;
	}
	//qDebug() << pA.getName();
	
	//PopulateTreeView();
	cboFilterChanged();
	
	return ret;
}
void MainWindowImpl::PopulateTreeView()
{
	QStringList TreeLine;
	trvList->clear();
	m_Items.clear();
	QTreeWidgetItem * trw;
	
	for ( int i = 0; i < m_AnimeList.count() ; i ++ )
	{
		TreeLine << QString::number( (m_AnimeList[i]).getNoSeq() ) << (m_AnimeList[i]).getName() ;
		m_Items.append(new QTreeWidgetItem((QTreeWidget*)0, TreeLine ));
		TreeLine.clear();
	}
	
	trvList->insertTopLevelItems(0, m_Items);
	
	if ( m_Items.size() > 0 )
	{
		trw = trvList->topLevelItem ( 0 );
		trvList->setCurrentItem(trw);
	}
}

void MainWindowImpl::PopulateTreeView( int pFilter )
{
	QStringList TreeLine;
	trvList->clear();
	m_Items.clear();	
	QList<animeclass> FilterList;
	
	switch ( pFilter )
	{
		case 1 : 	FilterList = m_SearchList;
					break;
		
		case 2 :	for ( int i = 0; i < m_AnimeList.count() ; i ++ )
					{
						if ( m_AnimeList[i].getTypeData() == 1 || m_AnimeList[i].getTypeData() == 2 || m_AnimeList[i].getTypeData() == 3 )
							 FilterList.append(m_AnimeList[i]);
					}
					break;
		
		case 3 :	for ( int i = 0; i < m_AnimeList.count() ; i ++ )
					{
						if ( m_AnimeList[i].getTypeData() == 6 )
							 FilterList.append(m_AnimeList[i]);
					}
					break;
					
		case 4 :	for ( int i = 0; i < m_AnimeList.count() ; i ++ )
					{
						if ( m_AnimeList[i].getTypeData() == 4  || m_AnimeList[i].getTypeData() == 7 )
							 FilterList.append(m_AnimeList[i]);
					}
					break;
					
		case 5 :	for ( int i = 0; i < m_AnimeList.count() ; i ++ )
					{
						if ( m_AnimeList[i].getTypeData() == 5 )
							 FilterList.append(m_AnimeList[i]);
					}
					break;
					
		case 6 :	for ( int i = 0; i < m_AnimeList.count() ; i ++ )
					{
						if ( m_AnimeList[i].getGenreData1() == 1 || m_AnimeList[i].getGenreData2() == 1 )
							 FilterList.append(m_AnimeList[i]);
					}
					break;
					
		case 7 :	for ( int i = 0; i < m_AnimeList.count() ; i ++ )
					{
						if ( m_AnimeList[i].getGenreData1() == 2 || m_AnimeList[i].getGenreData2() == 2 )
							 FilterList.append(m_AnimeList[i]);
					}
					break;
					
		case 8 :	for ( int i = 0; i < m_AnimeList.count() ; i ++ )
					{
						if ( m_AnimeList[i].getGenreData1() == 3 || m_AnimeList[i].getGenreData2() == 3 )
							 FilterList.append(m_AnimeList[i]);
					}
					break;
					
		case 9 :	for ( int i = 0; i < m_AnimeList.count() ; i ++ )
					{
						if ( m_AnimeList[i].getGenreData1() == 8 || m_AnimeList[i].getGenreData2() == 8 )
							 FilterList.append(m_AnimeList[i]);
					}
					break;
	}
	
	for ( int i = 0; i < FilterList.count() ; i ++ )
	{
		TreeLine << QString::number( (FilterList[i]).getNoSeq() ) << (FilterList[i]).getName() ;
		m_Items.append(new QTreeWidgetItem((QTreeWidget*)0, TreeLine ));
		TreeLine.clear();
	}
	
	trvList->insertTopLevelItems(0, m_Items);
	
}

void MainWindowImpl::cboFilterChanged()
{
	
	if ( cboFilter->currentIndex() == 0 )
		PopulateTreeView();
	else
		PopulateTreeView( cboFilter->currentIndex() );
	
	
	//QTreeWidgetItem * QTreeWidget::topLevelItem ( int index )	
	
}

void MainWindowImpl::cboProfilChanged()
{
	int ind = 0;
	QString Filter;
	
	// Load profile
	m_CurrentProfile = m_AnimeShellRc->GetQSettings(cboProfil->currentText());
	
	// Search filter index
	ind = cboFilter->findText(m_CurrentProfile[filter]);
	
	cboFilter->setCurrentIndex(ind);
	
}
//
